name: Update Podman version on Podman.io

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version to build and upload (e.g. "v9.8.7")'
        required: true

jobs:
  bump:
    name: Bump
    runs-on: ubuntu-22.04
    steps:
      - name: Get version
        id: getversion
        run: |

          if [[ -z "${{ inputs.version }}" ]]
          then
                VERSION=${{ github.event.release.tag_name }}
          else
                VERSION=${{ inputs.version }}
          fi

          # strip out the prefix v if it's there
          if [[ $VERSION == v* ]]; then
            VERSION="${VERSION:1}"
          fi
          echo "Bump to ${VERSION}"

          if [[ $VERSION != *-rc* ]] && [[ $VERSION != *-dev ]]; then
            echo "notRC=true" >> "$GITHUB_OUTPUT"
          else
            echo "SKIPPING: Version is a RC or a dev, no need to update."
          fi
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

      - name: Check open PRs
        if: steps.getversion.outputs.notRC == 'true'
        id: checkpr
        run: |
          prs=$(gh pr list \
            --repo containers/podman.io \
            --head bump-podmanv${{ steps.getversion.outputs.version }} \
            --state open \
            --json title \
            --jq 'length')
          if ((prs > 0)); then
            echo "SKIPPING: PR already exists to update to v${{ steps.getversion.outputs.version }}."
          else
            echo "prexists=false" >> "$GITHUB_OUTPUT"
          fi
        env:
          GH_TOKEN: ${{ secrets.PODMANBOT_TOKEN }}

      - uses: actions/checkout@v4
        if: >-
            steps.getversion.outputs.notRC == 'true' &&
            steps.checkpr.outputs.prexists == 'false'
        with:
          repository: containers/podman.io
          ref: refs/heads/main
          token: ${{ secrets.PODMANBOT_TOKEN }}

      - name: Check version
        if: >-
            steps.getversion.outputs.notRC == 'true' &&
            steps.checkpr.outputs.prexists == 'false'
        id: checkversion
        run: |
          # Check if version is actually higher than one on podman.io
          prevversion=`grep -P "(?<=export const LATEST_VERSION = ')(\d.\d.\d)" -o static/data/global.ts`
          echo "Version currently on site: ${prevversion}"
          echo "Version to update to: ${{ steps.getversion.outputs.version }}"
          # sort -V -C returns 0 if args are ascending version order
          if echo "${prevversion},${{ steps.getversion.outputs.version }}" | tr ',' '\n' | sort -V -C && [[ ${prevversion} != ${{ steps.getversion.outputs.version }} ]]
          then
            echo "needsUpdate=true" >> $GITHUB_OUTPUT
            echo "This release is a higher version, so we need to update podman.io"
          else
            echo "SKIPPING: This release is not a higher version, no need to update."
          fi

      - name: Bump version
        if: >-
          steps.getversion.outputs.notRC == 'true' &&
          steps.checkversion.outputs.needsUpdate == 'true' &&
          steps.checkpr.outputs.prexists == 'false'
        run: |
          # Replace the version in static/data/global.ts file
          sed -i "s/export const LATEST_VERSION = '.*';/export const LATEST_VERSION = '${{ steps.getversion.outputs.version }}';/g" static/data/global.ts
          echo "Updated file:"
          cat static/data/global.ts

      - name: Open PR
        if: >-
            steps.getversion.outputs.notRC == 'true' &&
            steps.checkversion.outputs.needsUpdate == 'true' &&
            steps.checkpr.outputs.prexists == 'false'
        run: |
          # Make commiter the user who triggered the action, either through cutting a release or manual trigger
          # GitHub gives everyone a noreply email associated with their account, use that email for the sign-off
          git config --local user.name ${{ github.actor }}
          git config --local user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"
          bumpbranch="bump-podmanv${{ steps.getversion.outputs.version }}"
          git checkout -b $bumpbranch
          git add static/data/global.ts
          git commit --signoff -m "Bump Podman to v${{ steps.getversion.outputs.version }}"
          git remote -v
          git remote add podmanbot https://github.com/podmanbot/podman.io
          git push podmanbot "+$bumpbranch"
          gh pr create \
            --title "Bump Podman to v${{ steps.getversion.outputs.version }}" \
            --body  "Bump Podman to v${{ steps.getversion.outputs.version }}" \
            --head "podmanbot:$bumpbranch" \
            --base "main" -R "containers/podman.io"
        env:
          GH_TOKEN: ${{ secrets.PODMANBOT_TOKEN }}
